package org.metis4j.framework.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class LoggerUtil {

	public static StackTraceElement findCaller() {
		StackTraceElement[] callStack = Thread.currentThread().getStackTrace();
		if (callStack == null)
			return null;
		StackTraceElement caller = null;
		String logClassName = LoggerUtil.class.getName();
		boolean isEachLogClass = false;
		StackTraceElement[] arrayOfStackTraceElement1 = callStack;
		int j = callStack.length;
		for (int i = 0; i < j; i++) {
			StackTraceElement s = arrayOfStackTraceElement1[i];
			if (logClassName.equals(s.getClassName())) {
				isEachLogClass = true;
			}
			if ((!isEachLogClass) || (logClassName.equals(s.getClassName())))
				continue;
			isEachLogClass = false;
			caller = s;
			break;
		}
		return caller;
	}

	private static Logger logger() {
		StackTraceElement caller = findCaller();
		if (caller == null) {
			return LoggerFactory.getLogger(LoggerUtil.class);
		}
		Logger log = LoggerFactory.getLogger(caller.getClassName() + "."
				+ caller.getMethodName() + " L " + caller.getLineNumber());
		return log;
	}

	public static void trace(String msg) {
		trace(msg, null);
	}

	public static void trace(String msg, Throwable e) {
		logger().trace(msg, e);
	}

	public static void debug(String msg) {
		debug(msg, null);
	}

	public static void debug(String msg, Throwable e) {
		logger().debug(msg, e);
	}

	public static void info(String msg) {
		info(msg, null);
	}

	public static void info(String msg, Throwable e) {
		logger().info(msg, e);
	}

	public static void warn(String msg) {
		warn(msg, null);
	}

	public static void warn(String msg, Throwable e) {
		logger().warn(msg, e);
	}

	public static void error(String msg) {
		error(msg, null);
	}

	public static void error(String msg, Throwable e) {
		logger().error(msg, e);
	}

}
